package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.result.R;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@EnableTransactionManagement
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /**
     * 分页查询所有套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        Page result = setmealService.getPageWithGategory(page, pageSize, name);
        return R.success(result);
    }

    /**
     * 修改套餐：信息回显
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.selectById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐：保存修改后的信息
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }


    /**
     * 删除+批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 停用+启用+批量停用+批量启用
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R soldAndStop(@RequestParam List<Long> ids,@PathVariable Integer status){
        setmealService.soldAndStop(ids);
        return R.success("修改成功");
    }

    /**
     * 手机App中，通过套餐id,status，查询套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
